package com.zhang.learns.netty;

import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.Buffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Scanner;

public class BioExample {

    private int serverPort = 8115;

    /**
     * 服务端
     *
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        ServerSocket serverSocket = new ServerSocket(serverPort);
        System.out.println("服务端启动");
        while (true) {
            Socket socket = serverSocket.accept();
            new Thread("test") {
                @Override
                public void run() {
                    try {
                        while (true) {
                            InputStream inputStream = socket.getInputStream();
                            byte[] bytes = new byte[256];
                            int len = 0;

                            byte[] resultBytes = new byte[0];
                            while ((len = inputStream.read(bytes)) != -1) {
                                resultBytes = Bytes.concat(resultBytes, Arrays.copyOf(bytes, len));
                            }
                            String result = new String(resultBytes, StandardCharsets.UTF_8);
                            System.out.println("服务端收到消息:" + result);

                            OutputStream outputStream = socket.getOutputStream();
                            
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            };
        }
    }

    /**
     * 客户端
     */
    @Test
    public void test2() throws IOException {
        Socket socket = new Socket("127.0.0.1", serverPort);
        socket.getOutputStream();
    }

    @Test
    public void test3() {

        for (int a = 0;a<10;a++) {
            System.out.println("a= " +a);
        }

        tag:
        for(int i = 0; i<10;i++){
            for (int j = 0;j<10;j++) {
                if (i == 5 && j ==5) {
                    break tag;
                }
                System.out.println("i=" + i + " j=" + j);
            }
        }
        System.out.println("1111");
    }

    @Test
    public void test4() {
        AA a = new AA();
        if (a.i==0) {
            System.out.println(1);
        }else if (a.j==0) {
            System.out.println(2);
        }

    }
    public static class AA{
        private int i=0;
        private int j=0;
    }
}
